﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using FoderSyncLib;

namespace FolderSyncApp
{
    public class FolderSyncConfig
    {
        private XmlDocument _configFile;
        public string FolderSyncParam;
        public string FtpServer;
        public string Username;
        public string Password;

        public FolderSyncConfig()
        {
            try
            {
                _configFile = new XmlDocument();
                _configFile.Load("FolderSyncService.exe.config");
                ReadConfig();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }

        /// <summary>
        /// Read Config
        /// </summary>
        public void ReadConfig()
        {
            XmlNode node = _configFile.DocumentElement;
            foreach (XmlNode node1 in node.ChildNodes)
                if (node1.Name == "appSettings")
                {
                    foreach (XmlNode param in node1.ChildNodes)
                    {
                        switch (param.Attributes["key"].Value)
                        {
                            case "foldersync":
                                FolderSyncParam = param.Attributes["value"].Value;
                                break;
                            case "ftpserver":
                                FtpServer = param.Attributes["value"].Value;
                                break;
                            case "username":
                                Username = param.Attributes["value"].Value;
                                break;
                            case "password":
                                Password = Helper.DeCrypt(param.Attributes["value"].Value, "12345");
                                break;
                        }
                    }
                }
        }

        /// <summary>
        /// Update Config
        /// </summary>
        public void UpdateConfig()
        {
            XmlNode node = _configFile.DocumentElement;
            foreach (XmlNode node1 in node.ChildNodes)
                if (node1.Name == "appSettings")
                {
                    foreach (XmlNode param in node1.ChildNodes)
                    {
                        switch (param.Attributes["key"].Value)
                        {
                            case "foldersync":
                                param.Attributes["value"].Value = FolderSyncParam;
                                break;
                            case "ftpserver":
                                param.Attributes["value"].Value = FtpServer;
                                break;
                            case "username":
                                param.Attributes["value"].Value = Username;
                                break;
                            case "password":
                                param.Attributes["value"].Value = Helper.EnCrypt(Password, "12345");
                                break;
                        }
                    }

                }
            _configFile.Save("FolderSyncService.exe.config");
        }

        public void Refresh()
        {
            _configFile.Load("FolderSyncService.exe.config");
            ReadConfig();
        }
    }
}
